package myky.com.doctorapp.ui

import android.content.Context
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gyf.barlibrary.ImmersionBar
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.activity_author__list_item.*
import kotlinx.android.synthetic.main.activity_my_docotor.*
import kotlinx.android.synthetic.main.public_common_title.*
import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.MyDocotorAdapter
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.MyDoctorContract
import myky.com.doctorapp.entity.MyDoctorEntity
import myky.com.doctorapp.entity.PageEntity
import myky.com.doctorapp.presenter.MyDoctorPresenter
import myky.com.doctorapp.utils.Constants
import myky.com.doctorapp.utils.ToastUtils


/**
 * @Description:  我的医生
 * @author: xiangyao
 * @date:   2019/3/25 11:08 AM
 * @version V1.0
 */
class MyDocotorActivity : BaseActivity<MyDoctorPresenter, List<MyDoctorEntity.DataBean>>(), MyDoctorContract.View, BaseQuickAdapter.OnItemChildClickListener, OnRefreshLoadMoreListener {


    lateinit var myDocotorAdapter: MyDocotorAdapter
    lateinit var doctors: ArrayList<MyDoctorEntity.DataBean>

    var currentPage = 1

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        currentPage++


        mPresenter.findMyDoctorList("v1.0", currentPage.toString(), "10")
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {

        currentPage = 1

        doctors.clear()

        mPresenter.findMyDoctorList("v1.0", currentPage.toString(), "10")
    }

    override fun reloadActivity() {
    }

    override fun injectTarget(): View = findViewById(R.id.activity_author_list_smartRefresh)
    override fun initData() {

        mPresenter.findMyDoctorList("v1.0", currentPage.toString(), "10")
    }

    override fun initView() {


        ImmersionBar.with(this).barColorInt(ContextCompat.getColor(this, R.color.colorPrimary)).titleBar(R.id.commonTitleLayout).navigationBarEnable(false).init()


        conmonTitleTextView.text = "我的医生"

        doctors = ArrayList<MyDoctorEntity.DataBean>()

        activity_author_list_recycler.layoutManager = LinearLayoutManager(this)

        myDocotorAdapter = MyDocotorAdapter(doctors)

        activity_author_list_recycler.adapter = myDocotorAdapter

        backLayout.setOnClickListener { finish() }

        myDocotorAdapter.onItemChildClickListener = this


        activity_author_list_smartRefresh.setOnRefreshLoadMoreListener(this)

    }

    override fun getContenView(): Int = R.layout.activity_my_docotor
    override fun showDataSuccess(datas: List<MyDoctorEntity.DataBean>?) {

        activity_author_list_smartRefresh.finishRefresh()
        activity_author_list_smartRefresh.finishLoadMore()

        if (datas != null) {
            datas.let {
                doctors.addAll(it)
            }
            myDocotorAdapter.notifyDataSetChanged()
        }

        if (mPageEntity.pages > currentPage) {

            activity_author_list_smartRefresh.setEnableLoadMore(true)

        } else {

            activity_author_list_smartRefresh.setEnableLoadMore(false)

        }

    }

    override fun getPresenter(): MyDoctorPresenter {
        return MyDoctorPresenter()
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        when(view?.id){
            R.id.author_list_item_header,R.id.author_list_item_click_lin->{

                val data = myDocotorAdapter.data

                val doctorid = data[position].doctorid

                var bundle=Bundle()

                bundle.putInt(Constants.DOCTORID,doctorid)

                startActivity(DoctorDetailsActivity::class.java,bundle)

            }

            R.id.author_list_item_check->{
                var casePermission = 0
                if (doctors.get(position).getCasepermission() === 0) {
                    casePermission = 1
                } else if (doctors.get(position).getCasepermission() === 1) {
                    casePermission = 0
                }

                mPresenter.allowedPermission(casePermission.toString(),doctors[position].doctorid.toString(),"v1.0",position)
            }


        }


    }

    override fun showAllowedPermissionResult(position: Int) {

        if (doctors.get(position).casepermission === 0) {

            doctors.get(position).casepermission = 1

        } else if (doctors.get(position).casepermission === 1) {

            doctors.get(position).casepermission = 0
        }

        myDocotorAdapter.notifyItemChanged(position)

    }


}
